package com.bdqn.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.pojo.Right;
import com.bdqn.pojo.Role;
import com.bdqn.service.RoleService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.awt.print.Pageable;
import java.util.List;

@Controller
@RequestMapping(value = "/role")
public class RoleController {
    @Resource
    private RoleService roleService;
    @RequestMapping(value="/list")
    public String list(@RequestParam(required = false,defaultValue = "")String roleName, @RequestParam(required = false,defaultValue = "1")int pageIndex, Model model){
        Sort sort=new Sort(Sort.Direction.ASC,"roleId");
        Pageable pageable=new PageRequest.of(pageIndex-1,3,sort);
        Page<Role> rolePage= roleService.findRole(roleName,pageable);
        model.addAttribute("rolePage",rolePage);
        model.addAttribute("pageable",pageable);
        return "role/list";
    }
    @RequestMapping(value = "/add")
    public String add(Model model){
        List<Right> rights=roleService.findAllRights();
        model.addAttribute("rights",rights);
        return "role/add";
    }
}
